import { getSearchHot, getSearchHotDetail, search } from '../../api/services/api'
export default ({
    namespaced: true,
    state: {
        hotSearchList: [],
        // type: 搜索类型；默认为 1 即单曲 , 取值意义 : 1: 单曲, 10: 专辑, 100: 歌手, 1000: 歌单, 1002: 用户, 1004: MV, 1006: 歌词, 1009: 电台, 1014: 视频, 1018:综合
        searchType: {
            keywords: '',
            limit: 15,
            currentPage: 1,
            type: 1,

        },
        //滚动单向锁
        prevY: 0,
        total: 0,
        musicList: {
            count: 0,
            data: [],
        },
        albumList: {
            count: 0,
            data: [],
        },
        mvsList: {
            count: 0,
            data: [],
        },
        singerList: {
            count: 0,
            data: [],
        },
        playList: {
            count: 0,
            data: [],
        },
        videoList: {
            count: 0,
            data: [],
        },
    },
    mutations: {
        // 改变单向锁
        changePrevY(state, val) {
            state.prevY = val;
        },
        changeHotSearchList(state, val) {
            state.hotSearchList = [...val];
        },
        changeMusicList(state, val) {
            console.log('当前搜索类型', state.searchType.type);
            switch (state.searchType.type) {
                case 1:
                    state.musicList.count = val.songCount;
                    state.musicList.data = val.songs;
                    state.total = val.songCount;
                    break;
                case 100:
                    state.singerList.count = val.artistCount;
                    state.singerList.data = val.artists;
                    state.total = val.artistCount;
                    break;
                case 10:
                    state.albumList.count = val.albumCount;
                    state.albumList.data = val.albums;
                    state.total = val.albumCount;
                    break;
                case 1014:
                    state.videoList.count = val.videoCount;
                    state.videoList.data = val.videos;
                    state.total = val.videoCount;
                    break;
                case 1004:
                    state.mvsList.count = val.mvCount;
                    state.mvsList.data = val.mvs;
                    state.total = val.mvCount;
                    break;
                case 1000:
                    console.log('进入了歌单');
                    state.playList.count = val.playlistCount;
                    state.playList.data = val.playlists;
                    state.total = val.playlistCount;
                    break;
                default:
                    break;
            }
        },
        changeCurrentPage(state, val) {
            state.searchType.currentPage = val;
        },
        changeLookType(state, val) {
            console.log(val);
            state.searchType.currentPage = 1;
            state.searchType.type = val - 0;
        },
    },
    actions: {
        async getSearchHot(context, val) {
            const data = await getSearchHot();
            // console.log('热搜', data);
            context.commit('changeHotSearchList', data.result.hots);
        },
        async getSearchHotDetail() {
            const data = await getSearchHotDetail()
            // console.log('热搜详情', data);
        },
        async search(context, val) {
            const { limit, currentPage, type } = context.state.searchType;
            console.log(val, limit, currentPage, type);
            const data = await search(val, limit, currentPage, type)
            console.log('搜索详情', data.result);
            context.commit('changeMusicList', data.result);
        }
    },

})
